package com.yzcrm.erp.finance.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.finance.entity.FinancePurchase;
import com.yzcrm.erp.finance.service.FinancePurchaseService;
import com.yzcrm.erp.psi.service.PsiEnterLeaveStockService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.R;

/**
 * @author jiang.xue
 * 采购申请
 */
@Api(remark = "财务：采购申请")
public class FinancePurchaseController extends Controller{

    @Inject
    private FinancePurchaseService financePurchaseService;

    /**
     * @author jiang.xue
     * 查看列表页
     */
    @Permissions({"finance:normalPurchase:index", "finance:projectPurchase:index", "finance:devPurchase:index",
            "finance:otherPurchase:index", "finance:officePurchase:index"})
    @ActionApi(summary = "检索采购申请列表数据", remark="检索采购申请列表数据")
    @Params({
            @Param(name="sceneId", remark="场景Id", dataType="int", defaultValue="", required=false),
            @Param(name="type", remark="权限标识 label", dataType="int", defaultValue="", required=true)
    })

    public void queryPageList(BasePageRequest basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject();
        basePageRequest.setJsonObject(jsonObject);
        renderJson(financePurchaseService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * @author jiang.xue
     * 添加或者修改采购申请
     */
    @ActionApi(summary = "添加或者修改采购申请", remark="添加或者修改采购申请")
    @Permissions({"finance:normalPurchase:save", "finance:projectPurchase:save", "finance:devPurchase:save",
            "finance:otherPurchase:save", "finance:officePurchase:save",
            "finance:normalPurchase:update", "finance:projectPurchase:update", "finance:devPurchase:update",
            "finance:otherPurchase:update", "finance:officePurchase:update"})
    public void saveOrUpdate(){

        JSONObject jsonObject = JSON.parseObject(getRawData());
        renderJson(financePurchaseService.addOrUpdate(jsonObject));
    }

    /**
     * @author jiang.xue
     * 根据id删除采购申请
     */
    @ActionApi(summary = "根据id删除采购申请", remark="根据id删除采购申请")
    @Params({
            @Param(name="purchaseIds", remark="采购申请ID 多个ID,逗号分割", dataType="String", defaultValue="", required=true)
    })
    @Permissions({"finance:normalPurchase:delete", "finance:projectPurchase:delete", "finance:devPurchase:delete",
            "finance:otherPurchase:delete", "finance:officePurchase:delete"})
    @NotNullValidate(value = "purchaseIds", message = "采购申请id不能为空")
    public void deleteByIds(@Para("purchaseIds") String purchaseIds) {
        renderJson(financePurchaseService.deleteByIds(purchaseIds));
    }

    /**
     * @author jiang.xue
     * 根据采购申请id查询
     */
    @ActionApi(summary = "根据采购申请id查询", remark="根据采购申请id查询")
    @Params({
            @Param(name="purchaseId", remark="采购申请ID", dataType="String", defaultValue="", required=true)
    })
    @Permissions({"finance:normalPurchase:read", "finance:projectPurchase:read", "finance:devPurchase:read",
            "finance:otherPurchase:read", "finance:officePurchase:read"})
    @NotNullValidate(value = "purchaseId", message = "采购申请id不能为空")
    public void queryById(@Para("purchaseId") Integer purchaseId) {
        renderJson(R.ok().put("data", financePurchaseService.queryById(purchaseId)));
    }

    /**
     * @author jiang.xue
     * 查询采购申请明细列表
     */
    @ActionApi(summary = "查询采购申请明细列表", remark="查询采购申请明细列表，编辑采购申请时使用")
    @Params({
            @Param(name="purchaseId", remark="采购申请Id", dataType="Integer", defaultValue="", required=true)
    })
    public void queryEnterLeaveStockDetailByElsId(@Para("purchaseId") Integer purchaseId) {
        renderJson(R.ok().put("data", financePurchaseService.listPurchaseDetailByPurchaseId(purchaseId)));
    }


    /**
     * @author jiang.xue
     * 采购编号查询
     */
    @ActionApi(summary = "采购编号查询", remark="采购编号查询")
    @Params({@Param(name="purchaseNum", remark="采购编号", dataType="String", defaultValue="", required=false),
            @Param(name="label", remark="模块编号", dataType="Integer", defaultValue="", required=false)
    })
    public void queryPurchaseNum(@Para("purchaseNum") String purchaseNum, @Para("label") Integer label) {

        if(ObjectUtil.isNotEmpty(purchaseNum)) {

            purchaseNum = purchaseNum.trim();
        }
        renderJson(financePurchaseService.queryPurchaseNum(purchaseNum, label));
    }

}
